<?php
	require_once(dirname(__FILE__) . "/../core/safeParam.php");

	$page = 1;
	
	// how many records per page
	$size = 10;
	
	// we get the current page from $_GET
	if (isset($_GET['pagenum'])) {
  		$page = (int) $_GET['pagenum'];
	} 
	
	$order = " order by options.rid desc ";
	$extra_search = "";
	
	if ($_REQUEST['by'] == 'name') {
		$extra_search = "  WHERE config.sitename  LIKE '%" . $_REQUEST['search-city'] . "%' ";
  		$order        = " order by config.sitename ";
	} else {
		if ($_REQUEST['search-city'] != "" && $_REQUEST['search-category'] != "") {
			$extra_search = "  WHERE (name = 'area' AND value LIKE '" . $_REQUEST['search-city'] . "') AND (name = 'category' AND value = '" . $_REQUEST['search-category'] . "') ";
    		$order        = " order by config.sitename ";
  		} else {
  			if ($_REQUEST['search-city'] != "") {
  				$extra_search = "  WHERE (name = 'area' AND value LIKE '" . $_REQUEST['search-city'] . "') ";
      		$order        = " order by config.sitename ";
    		}
    	
    		if ($_REQUEST['search-category'] != "") {
    			$extra_search = " WHERE (name = 'category' AND value = '" . $_REQUEST['search-category'] . "')  ";
      		$order        = " order by config.sitename ";
    		} 
  		}
	}

	if (getOption('cb-show-main-rb', RID) != 1) {
  		$includerb = " AND config.id != 1 ";
	} else {
  		$includerb = "";
	}
	
	
	$query = "SELECT * from options INNER JOIN config ON options.rid = config.id " . $extra_search . "  " . $includerb . " GROUP BY config.sitename $order";
		
	$pagination = new Pagination();
	$pagination->setLink("index.php?pagenum=%s&search-category=" . $_REQUEST['search-category'] . "");
	$pagination->setPage($page);
	$pagination->setSize($size);
	$total = count($database->select($query));
	$pagination->setTotalRecords($total);
	$search_results = $database->select($query . $pagination->getLimitSql());
	
	$contadorOpen=0;
	$contadorClose=0;
	$contadorDestacados=0;
	
	for ($i = 0; $i < count($search_results); $i++) {
		$search_results[$i]['locations']  = $database->select("SELECT * from locations where rid = '" . $search_results[$i]['rid'] . "'");
  		$search_results[$i]['categories'] = $database->select("SELECT * from menu_cat  where rid = '" . $search_results[$i]['rid'] . "'");
  			
  		
  		if(isDestacado($search_results[$i])){
  			if(isOpen($search_results[$i])) {
  				$search_results[$i]['status'] = "open";
  				$destacados[$contadorDestacados] = $search_results[$i];
  				$contadorDestacados++;
 			} else {
 				$search_results[$i]['status'] = "closed";
 				$destacados[$contadorDestacados] = $search_results[$i];
  				$contadorDestacados++;
 			}	
		} else {
			if(isOpen($search_results[$i])){
  				$search_results[$i]['status'] = "open";
  				$search_results_open[$contadorOpen] = $search_results[$i];
  				$contadorOpen++; 
			} else {
				$search_results[$i]['status'] = "closed";
				$search_results_close[$contadorClose] = $search_results[$i];	
				$contadorClose++;
			}
		}	
	} 
	
	$j=0;
	for($i=0; $i<$contadorOpen; $i++){
		$search_results_order[$j] = $search_results_open[$i];
		$j++;
	}
	

	for($i=0; $i<$contadorClose; $i++){
			$search_results_order[$j] = $search_results_close[$i];
			$j++;
	}
				
	$smarty->assign('total', $total);
	$smarty->assign('pagination', $pagination->create_links());
	$smarty->assign("search_results", $search_results);
	$smarty->assign("search_destacados", $destacados);
	$smarty->assign("search_results_open", $search_results_open);
	$smarty->assign("search_results_close", $search_results_close);
?>
